package com.ax.center.service.impl;

import cn.hutool.core.util.BooleanUtil;
import com.ax.center.dto.ApiResponse;
import com.ax.center.entity.Role;
import com.ax.center.entity.UserAuth;
import com.ax.center.entity.UserRole;
import com.ax.center.entity.Users;
import com.ax.center.mapper.RoleMapper;
import com.ax.center.mapper.UserRoleMapper;
import com.ax.center.mapper.UsersMapper;
import com.ax.center.service.UserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zy
 * @date 2023/7/5 16:11
 */

@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper,UserRole> implements UserRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public List<Role> queryRoleNameByUserId(Long userId) {
        List<UserRole> userRoles = query().eq("user_id", userId).list();
        List<Long> roleIds = userRoles.stream().map(UserRole::getRoleId).collect(Collectors.toList());
        List<Role> roles = roleMapper.selectBatchIds(roleIds);
        return roles;
    }

    @Override
    public ApiResponse insertRole(Long userId,Long roleId) {
        if (query().eq("user_id",userId).eq("role_id",roleId).count()>0) {
            return  ApiResponse.fail("新增失败,角色已存在");
        }
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);
        save(userRole);
        return ApiResponse.ok("新增成功");
    }

    @Override
    public ApiResponse deleteOneByRoleId( Long roleId) {
        QueryWrapper<UserRole> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("role_id",roleId);
        userRoleMapper.delete(roleQueryWrapper);
        return ApiResponse.ok("删除成功");
    }




    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<Long> queryByRoleIds(String  roleName) {
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("role_name",roleName);
        List<Role> roles = roleMapper.selectList(roleQueryWrapper);
        List<Long> roleIds = roles.stream().map(Role::getRoleId).collect(Collectors.toList());
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.in("role_id",roleIds);
        List<UserRole> userRoles = userRoleMapper.selectList(userRoleQueryWrapper);
        List<Long> userIds = userRoles.stream().map(UserRole::getUserId).collect(Collectors.toList());
        return userIds;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResponse updateRole(Long id, Long roleId) {
        QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
        usersQueryWrapper.eq("staff_id",id);
        Users users = usersMapper.selectOne(usersQueryWrapper);
        Long usersId = users.getId();
        boolean update = update().set("role_id", roleId).eq("user_id", usersId).update();
        if(BooleanUtil.isTrue(update)){
            return ApiResponse.ok("修改成功");
        }else {
            return ApiResponse.fail("修改失败");
        }
    }

    @Override
    public ApiResponse deleteByUserId(Long userId) {
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.in("user_id",userId);
        userRoleMapper.delete(userRoleQueryWrapper);
        return ApiResponse.ok("删除成功");
    }
}
